﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.ServiceHost;
using ServiceStack.Service;
using System.IO;
using ServiceStack.Text;
using System.Web;

namespace MMM.Services.Response
{
    public class ExcelFileResult : IHasOptions, IStreamWriter
    {
        private readonly Stream _responseStream;
        public IDictionary<string, string> Options { get; private set; }

        public ExcelFileResult(Stream responseStream, string Name)
        {
            _responseStream = responseStream;

            Options = new Dictionary<string, string> {
                 {"Content-Type", "application/octet-stream"},
                 {"Content-Disposition","attachment; filename=\""+HttpUtility.UrlEncode(Name,Encoding.GetEncoding("UTF-8"))+".xls\";"}
             };
        }

        public void WriteTo(Stream responseStream)
        {
            if (_responseStream == null)
                return;

            _responseStream.WriteTo(responseStream);
            responseStream.Flush();
        }
    }

}
